<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button
              type="success"
              icon="Plus"
              @click="router.push({ name: 'module_business_activity_form' })">
              {{ i18n('common.create') }}
            </el-button>
          </div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="general-situation">
        <div>
          <el-tag class="float-right p10 mr10" type="info" size="small" effect="dark" round>
            {{ totalPage }}
          </el-tag>
          <h5 class="float-right mr10">全部活动单</h5>
        </div>
      </div>

      <div class="admin_main_block_select color" style="padding: 0 20px;">
        <el-form label-position="right" label-width="auto">
          <el-row :gutter="20">
            <el-col :span="4">
              <el-form-item :label="i18n('activity.title')">
                <el-input v-model="dataForm.title" :placeholder="$t('common.please_input') + $t('activity.title')"  clearable @change="getDataList(model)"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item :label="i18n('product.is_hidden')">
                <el-radio-group v-model="dataForm.is_hidden" @change="getDataList(model)">
                  <el-radio label="">{{ i18n('common.all') }}</el-radio>
                  <el-radio v-for="(v, k) in statusList" :key="k" :label="v.value">
                    {{ v.text }}
                    <span class="red">({{ v.total }})</span>
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="4">
              <el-form-item :label="i18n('activity.client_id')">
                <el-select
                  v-model="dataForm.client_id"
                  :placeholder="i18n('common.please_select') + i18n('activity.client_id')" clearable @change="getDataList(model)">
                  <el-option v-for="(v, k) in clientList" :label="v.name" :key="k" :value="v.id">
                    <span class="float-left">{{ v.name }}</span>
                    <span class="float-right">{{ v.total }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="4">
              <el-form-item :label="i18n('activity.contact_id')">
                <el-select
                  v-model="dataForm.contact_id"
                  :placeholder="i18n('common.please_select') + i18n('activity.contact_id')" clearable @change="getDataList(model)">
                  <el-option v-for="(v, k) in contactList" :label="v.name" :key="k" :value="v.id">
                    <span class="float-left">{{ v.name }}</span>
                    <span class="float-right">{{ v.total }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column :label="i18n('activity.client_id')" min-width="150">
            <template v-slot="scope">
              <span v-if="scope.row.client">{{ scope.row.client.name }}</span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('activity.contact_id')" min-width="100">
            <template v-slot="scope">
              <span v-if="scope.row.contact">{{ scope.row.contact.name }}</span>
            </template>
          </el-table-column>

          <el-table-column prop="title" :label="i18n('activity.title')" min-width="240">
          </el-table-column>

          <el-table-column :label="$t('product.is_hidden')" width="80">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.is_hidden.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.no')"
                :inactive-text="$t('common.yes')"
                style="--el-switch-on-color: #ff4949; --el-switch-off-color: #13ce66"
                @change="handleStatus($event, scope.row.id, 'is_hidden')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column prop="end_time" :label="i18n('activity.end_time')" width="100">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="200">
            <template v-slot="scope">
              <el-button
                type="primary"
                icon="Edit"
                @click="router.push({ name: 'module_business_activity_form', query: { id: scope.row.id } })">
                {{ i18n('common.update') }}
              </el-button>

              <el-button
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="business-activity-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/activity')

  const clientList = ref([])
  const contactList = ref([])
  const statusList = ref([])

  const dataForm = reactive([
    'client_id',
    'contact_id',
    'title',
    'is_hidden',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const loadSearchList = () => {
    $http({
      url: $http.adornUrl('/business/activity/search/data'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({data}) => {
      if (data && data.status === 200) {
        clientList.value = data.data.client
        contactList.value = data.data.contact
        statusList.value = data.data.status
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    getDataList(model)

    loadSearchList()
  })
</script>
